package ui.panels;

import entities.ShiftRequest;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.DoctorDriver;
import manager.GenericDriver;
import model.ViewShiftRequestTableModel;
import ui.windows.RemedySchedulerFrame;
import ui.windows.ViewShiftRequestDialog;

public class EditShiftRequestPanel extends javax.swing.JPanel {

    /** Creates new form EditShiftRequestPanel */
    public EditShiftRequestPanel(RemedySchedulerFrame mainFrame) {
        this.mainFrame = mainFrame;
        driver = mainFrame.getGenericDriver();
        initComponents();
        includeModel();
    }

    private void includeModel() {
        model = new ViewShiftRequestTableModel(driver.showSRByEID(mainFrame.getUser().getUserID()));
        shiftRequestTable.setModel(model);
        shiftRequestTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        shiftRequestTable.setFillsViewportHeight(true);
        shiftRequestTable.getTableHeader().setFont(new Font(null, Font.BOLD, 11));
        shiftRequestTable.getTableHeader().setReorderingAllowed(false);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer1.setBackground(new Color(255,51,255));
        DefaultTableCellRenderer cellRenderer2 = new DefaultTableCellRenderer();
        cellRenderer2.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer2.setBackground(Color.yellow);
        shiftRequestTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer1);
        shiftRequestTable.getColumnModel().getColumn(4).setCellRenderer(cellRenderer2);
        generateRequest = new GenerateShiftRequestPanel(mainFrame, this);
        switchTarget = 1;
    }

    private void swithAction() {
        int choise = cbActions.getSelectedIndex();
        switchTarget(choise);
        cbActions.setSelectedIndex(0);
    }

    private void switchTarget(int choise) {
        if (switchTarget != choise) {
            switchTarget = choise;
            this.remove(viewShiftRequest);
            switch (switchTarget) {
                case 0:
                    break;
                case 1:
                    viewShiftRequest = new JScrollPane(shiftRequestTable);
                    break;
                case 2:
                    viewShiftRequest = new JScrollPane(generateRequest);
                    break;
            }
        }
        this.add(viewShiftRequest, BorderLayout.CENTER);
        this.revalidate();
    }

    public boolean createShiftRequest(ShiftRequest request) {
        request.setEmployeeId(mainFrame.getUser().getUserID());
        boolean returnVal = driver.insertShiftRequest(request);
        if (returnVal) {
            request.setModifiedDate(Calendar.getInstance().getTime());
            model.addElement(request);
        }
        return returnVal;
    }

//    public boolean updateShiftRequest(ShiftRequest request) {
//        boolean returnVal = driver.updateRequest(request);
//        if (returnVal) {
//            request.setModifiedDate(Calendar.getInstance().getTime());
//            model.update(request);
//        }
//        return returnVal;
//    }

//    private boolean deleteShiftRequest(ShiftRequest request) {
//        boolean returnVal = driver.deleteRequest(request);
//        if (returnVal) {
//            model.remove(request);
//        }
//        return returnVal;
//    }
//    
//    private void deleteAcion() {
//        if(selectedRequest != null) {
//            boolean returnVal = deleteShiftRequest(selectedRequest);
//            if(returnVal) {
//                JOptionPane.showMessageDialog(mainFrame, "Deleted");
//            } else {
//                JOptionPane.showMessageDialog(mainFrame, "Cannot delete !", "Error", JOptionPane.WARNING_MESSAGE);
//            }
//        } else {
//            JOptionPane.showMessageDialog(null, "You migh select one request !");
//        }
//    }

    private void setSelectedRequest(java.awt.event.MouseEvent evt) {
        int index = shiftRequestTable.getSelectedRow();
        if (index != -1) {
            selectedRequest = model.getAt(index);
            if (evt.getClickCount() == 2) {
                ViewShiftRequestDialog dialog = new ViewShiftRequestDialog(mainFrame);
                dialog.setLocationRelativeTo(null);
                dialog.setShiftRequest(selectedRequest);
                dialog.setVisible(true);
            }
        }
    }
    
    public void showAllRequest() {
        switchTarget(1);
    }
    
    public void showCreateShift() {
        switchTarget(2);
    }

    public void closeTarget() {
        switchTarget(1);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private GenericDriver driver;
    private ViewShiftRequestTableModel model;
    private GenerateShiftRequestPanel generateRequest;
    private ShiftRequest selectedRequest;
    private int switchTarget;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbActions = new javax.swing.JComboBox();
        viewShiftRequest = new javax.swing.JScrollPane();
        shiftRequestTable = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Actions", "View", "Generate" }));
        cbActions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(458, Short.MAX_VALUE)
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        shiftRequestTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        shiftRequestTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                shiftRequestTableMouseClicked(evt);
            }
        });
        viewShiftRequest.setViewportView(shiftRequestTable);

        add(viewShiftRequest, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void cbActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActionsActionPerformed
    swithAction();
}//GEN-LAST:event_cbActionsActionPerformed

private void shiftRequestTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_shiftRequestTableMouseClicked
    setSelectedRequest(evt);
}//GEN-LAST:event_shiftRequestTableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbActions;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTable shiftRequestTable;
    private javax.swing.JScrollPane viewShiftRequest;
    // End of variables declaration//GEN-END:variables
}
